package com.atguigu.lease.web.app.service.impl;


import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoMapper ApartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private BrowsingHistoryService browsingHistoryService;


    @Override
    public void queryPageRoom(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo, Long id) {
        roomInfoMapper.queryPageRoom(roomItemVoPage, queryVo, id);
    }

    @Override
    public RoomDetailVo queryRoomById(Long id, Long userId) {
        //查询房间
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        if (roomInfo == null) {
            return null;
        }
        //查询公寓
        //查询公寓基本信息
        ApartmentInfo apartmentInfo = ApartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //查询公寓标签
        List<LabelInfo> apartmentLabel = labelInfoMapper.querylabel(roomInfo.getApartmentId());
        //查询公寓图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, roomInfo.getApartmentId());
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        List<GraphInfo> graphInfoList = graphInfoMapper.selectList(graphInfoLambdaQueryWrapper);
        //查询公寓内最低的房间价格
        BigDecimal minRent = roomInfoMapper.queryMinRent(roomInfo.getApartmentId());
        //汇总vo
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        apartmentItemVo.setLabelInfoList(apartmentLabel);
        apartmentItemVo.setGraphVoList(graphInfoList);
        apartmentItemVo.setMinRent(minRent);


        //查询房间图片
        LambdaQueryWrapper<GraphInfo> graphInfoRoomLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoRoomLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoRoomLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        List<GraphInfo> graphInfoList1 = graphInfoMapper.selectList(graphInfoRoomLambdaQueryWrapper);
        //设置图片为空值时的判断，防止出现空指针，graphVos需定义在外  供if else 方法内操作同一对象
        List<GraphVo> graphVos;
        if (!CollectionUtils.isEmpty(graphInfoList1)) {
            graphVos = new ArrayList<>(graphInfoList1.size());
            GraphVo graphVo = new GraphVo();
            for (GraphInfo graphInfo : graphInfoList1) {
                graphVo.setName(graphInfo.getName());
                graphVo.setUrl(graphInfo.getUrl());
                graphVos.add(graphVo);
            }
        } else {
            graphVos = null;
        }


        //查询房间属性
        List<AttrValueVo> attrValueVoList = attrValueMapper.queryAttrValueById(id);
        //查询房间配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.queryFacilityById(id);
        //查询房间标签
        List<LabelInfo> labelInfoList = labelInfoMapper.queryRoomLable(id);
        //查询房间支付方式
        List<PaymentType> paymentTypeList = paymentTypeMapper.listById(id);
        //查询公寓（房间）杂费
        List<FeeValueVo> feeValueVoList = feeValueMapper.queryFeeById(roomInfo.getApartmentId());
        //查询租期
        List<LeaseTerm> leaseTermList = leaseTermMapper.queryLeaseById(id);
        //查看公寓房间是否有入住状态的租约信息
        LambdaQueryWrapper<LeaseAgreement> leaseAgreementLambdaQueryWrapper = new LambdaQueryWrapper<>();
        leaseAgreementLambdaQueryWrapper.eq(LeaseAgreement::getRoomId, roomInfo.getId());
        leaseAgreementLambdaQueryWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING, LeaseStatus.RENEWING);
        Long count = leaseAgreementMapper.selectCount(leaseAgreementLambdaQueryWrapper);
        //调用保存浏览历史 异步方法
        browsingHistoryService.saveHistory(id,userId);


        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);
        roomDetailVo.setIsCheckIn(count > 0);

        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(graphVos);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);

        return roomDetailVo;
    }

    @Override
    public void queryPageRoomById(Page<RoomItemVo> roomItemVoPage, Long id) {
        roomInfoMapper.queryPageRoomById(roomItemVoPage, id);
    }
}




